//
//  CCVSLabel.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 5/28/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSLabel.h"
#import "CCVSString.h"
#import "CCVSCommon.h"

@interface CCVSLabel ()

@property (nonatomic,weak) UIViewController * vc;
@property (nonatomic) int linesToDisplay;
@property (nonatomic,weak) UIColor * textColor;
@property (nonatomic,weak) UIColor * bgColor;

@end


@implementation CCVSLabel

@synthesize data = _data;
@synthesize vc = _vc;
@synthesize linesToDisplay = _linesToDisplay;
@synthesize textColor = _textColor;
@synthesize bgColor = _bgColor;
/////////////////////

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


-(BOOL)isExpandable
{
    return (self.linesToDisplay > 0);
}

//should return the frame size with width and height populated
+(CGSize)calcViewHeight:(CGFloat)viewWidth fromObject:(id<CCVSCustomDataType>)data
{
    CGSize size;
    NSString *text = ((CCVSString*)data).string;
    size = [text sizeWithFont:[UIFont fontWithName:@CCVS_FONTNAME size:CCVS_FONTSIZE]
            constrainedToSize:CGSizeMake(viewWidth, 9999)
                lineBreakMode:NSLineBreakByWordWrapping];
    if (!((CCVSString*)data).isExpanded) {
        int maxHeight = ((CCVSString*)data).linesToDisplay * [CCVSLabel referenceRect].height;
        if (size.height > maxHeight) {
            size.height = maxHeight + [CCVSLabel calcSizeOfMoreOption:@CCVS_EXPANSION_TEXT].height;
        }
    }
    return size;
}

+(CGSize)calcViewHeightWithOutExpansionText:(CGFloat)viewWidth
                                 fromObject:(id<CCVSCustomDataType>)data
                            maxLimitReached:(BOOL*)max

{
    *max = NO;
    CGSize size;
    NSString *text = ((CCVSString*)data).string;
    size = [text sizeWithFont:[UIFont fontWithName:@CCVS_FONTNAME size:CCVS_FONTSIZE]
            constrainedToSize:CGSizeMake(viewWidth, 9999)
                lineBreakMode:NSLineBreakByWordWrapping];
    if (!((CCVSString*)data).isExpanded) {
        int maxHeight = ((CCVSString*)data).linesToDisplay * [CCVSLabel referenceRect].height;
        if (size.height > maxHeight) {
            size.height = maxHeight;
            *max = YES;
        }
    }
    return size;
}

+(CGSize)referenceRect
{
    return [@"A" sizeWithFont:[UIFont fontWithName:@CCVS_FONTNAME size:CCVS_FONTSIZE]
            constrainedToSize:CGSizeMake(9999, 9999)
                lineBreakMode:NSLineBreakByWordWrapping];
}

+(CGSize)calcSizeOfMoreOption:(NSString*)text
{
    CGSize size = [text sizeWithFont:[UIFont fontWithName:@CCVS_EXPANSION_FONTNAME
                                                     size:CCVS_EXPANSION_FONTSIZE]
                   constrainedToSize:CGSizeMake(9999, 9999)
                       lineBreakMode:NSLineBreakByWordWrapping];
    return size;
}


//initialize the object...primary way it will be init'ed
-(id)initWithData:(id<CCVSCustomDataType>)data withFrame:(CGRect)frame viewController:(UIViewController*)vc
{
    self  = [super initWithFrame:frame];
    self.data = data;
    self.vc = (UIViewController<CCVSCustomViewDelegate> *)vc;
    self.linesToDisplay = ((CCVSString*)data).linesToDisplay;
    self.bgColor = ((CCVSString*)data).bgColor;
    self.textColor = ((CCVSString*)data).textColor;
    return self;    
}

//should build view to be displayed (would likely return self)
-(UIView*)buildView
{
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor clearColor];
    self.numberOfLines = 0;
    self.lineBreakMode = NSLineBreakByWordWrapping;
    self.font = [UIFont fontWithName:@CCVS_FONTNAME size:CCVS_FONTSIZE];
    ///
    NSString * string = ((CCVSString*)self.data).string;
    BOOL max;
    CGSize size = [CCVSLabel calcViewHeightWithOutExpansionText:self.frame.size.width fromObject:self.data maxLimitReached:&max];
    if (max) {
        //if we are here, we need some special handling.
        CGSize refsize = [CCVSLabel referenceRect];
        CGSize moreSize = [CCVSLabel calcSizeOfMoreOption:@CCVS_EXPANSION_TEXT];
        int len = (size.width / refsize.width) * (size.height / refsize.height);
        NSRange range = NSMakeRange(0, len-6); //remove some extra
        string = [string substringWithRange:range];
        self.text = string;
        ///
        UILabel * moreView = [self createLabelForExpansion:@CCVS_EXPANSION_TEXT
                                                 withFrame:CGRectMake(CCVS_BOARDERSIZE, self.frame.size.height - moreSize.height, moreSize.width, moreSize.height)];
        ///
        UITapGestureRecognizer * tap;
        if ([self willSegueOnEvent]) {
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureForLabel:)];
        } else {
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureForExpansion:)];
        }
        tap.numberOfTapsRequired = 1;
        [moreView addGestureRecognizer:tap];
        [self addSubview:moreView];
    } else {
        self.text = string;
    }
    ///
    return self;
}

-(BOOL)willSegueOnEvent //true/false if the view will require a segue
{
    if (self.data) return ((CCVSString*)self.data).segueInsteadOfExpand;
    return NO;
}

-(void)handleTapGestureForExpansion:(UITapGestureRecognizer *) gesture
{
    //proxy method for gesture to view controller with correct object
    [(id<CCVSCustomViewDelegate>)self.vc toggleExpantionAndRefreshView:self forDataType:self.data];
}

-(void)handleTapGestureForLabel:(UITapGestureRecognizer*)gesture
{
    //NSLog(@"Tap captured: %@",gesture);
    [(id<CCVSCustomViewDelegate>)self.vc segueToVCWith:self.data segueName:[self nameOfSegue:gesture]];
}


//if true above this should return the name of the segue
//if this was requested as a result of a gesture event
//the gesture will be provided as well
-(NSString*)nameOfSegue:(UIGestureRecognizer*)gesture
{
    //return @"LONG_TEXT_VC";
    return @MODAL_WEBVIEW_SEGUE;
}

//Creates the label...note if this is a hyperlink, the label is actually
//a CCSV Extended Label
-(UILabel*)createLabelForExpansion:(NSString*)data withFrame:(CGRect)frame
{
    
    UILabel * label1;
    NSString * text;
    //we have a regular label
    label1 = [[UILabel alloc] initWithFrame:frame];
    text = ((NSString*)data);
    label1.backgroundColor = [UIColor whiteColor];
    label1.textColor = [UIColor blackColor];
    label1.numberOfLines = 0;
    label1.lineBreakMode = NSLineBreakByWordWrapping;
    label1.font = [UIFont fontWithName:@CCVS_EXPANSION_FONTNAME size:CCVS_EXPANSION_FONTSIZE];
    
    //add the data to the label
    label1.text = text;
    
    label1.userInteractionEnabled = YES; //enable it for gestures
    
    return label1;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
